package cn.tanghom.support.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.tanghom.app.system.model.User;
import cn.tanghom.conf.CommonKey;

public class ContextHolder {
	static class  ContextData{
		public HttpSession session;
		public HttpServletRequest request;
		public HttpServletResponse response;
		public User user;
		
	};
    private static ThreadLocal<ContextData> sessionThreadLocal = new ThreadLocal<ContextData>() {

        @Override
        protected ContextData initialValue() {
            return new ContextData();
        }
    };

    public static void remove() {
        sessionThreadLocal.remove();
    }


    public static void setSession(HttpSession session) {
        sessionThreadLocal.get().session = session;
    }
    
    public static void setResponse(HttpServletResponse response) {
        sessionThreadLocal.get().response = response;
    }
    
    public static void setRequest(HttpServletRequest req) {
        sessionThreadLocal.get().request = req;
    }


    public static void setUser(User user) {
    	ContextData sess = sessionThreadLocal.get();
    	sess.user = user;
    }
    
    public static HttpSession getSession() {
        return sessionThreadLocal.get().session;
    }
    
    public static HttpServletResponse getResponse() {
        return sessionThreadLocal.get().response;
    }
    
    public static HttpServletRequest getRequest() {
        return sessionThreadLocal.get().request;
    }
    
    public static User getUser() {
    	ContextData sess = sessionThreadLocal.get();    	
        return (User)sess.user;
    }    
}
